<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学生信息管理系统</title>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            color: #333;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        h1 {
            color: #2c3e50;
            margin-bottom: 20px;
            text-align: center;
        }
        button {
            background-color: #3498db;
            color: white;
            border: none;
            padding: 12px 24px;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
            display: block;
            margin: 20px auto;
        }
        button:hover {
            background-color: #2980b9;
        }
        .loading {
            display: none;
            text-align: center;
            margin: 20px 0;
            color: #7f8c8d;
        }
        #result {
            margin-top: 20px;
            padding: 20px;
            background-color: #ecf0f1;
            border-radius: 4px;
            min-height: 50px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 15px;
        }
        th, td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }
        th {
            background-color: #f2f2f2;
            font-weight: bold;
        }
        tr:hover {
            background-color: #f5f5f5;
        }
        .error {
            color: #e74c3c;
            background-color: #fee;
            padding: 10px;
            border-radius: 4px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>学生信息管理系统</h1>
        <button id="fetchBtn">获取学生列表</button>
        <div class="loading" id="loading">加载中...</div>
        <div id="result">
            <p>点击上方按钮获取学生信息</p>
        </div>
    </div>

    <script>
        document.getElementById('fetchBtn').addEventListener('click', fetchStudents);
        
        async function fetchStudents() {
            const resultDiv = document.getElementById('result');
            const loadingDiv = document.getElementById('loading');
            
            // 显示加载状态
            resultDiv.innerHTML = '';
            loadingDiv.style.display = 'block';
            
            try {
                // 通过Nginx代理访问后端服务
                // 根据配置，Nginx监听8080端口
                const response = await fetch('http://localhost:8080/students', {
                    method: 'GET',
                    headers: {
                        'Content-Type': 'application/json'
                    }
                });
                
                if (!response.ok) {
                    throw new Error(`HTTP错误! 状态码: ${response.status}`);
                }
                
                const students = await response.json();
                
                // 隐藏加载状态
                loadingDiv.style.display = 'none';
                
                // 显示学生列表
                if (students && students.length > 0) {
                    displayStudents(students);
                } else {
                    resultDiv.innerHTML = '<p>暂无学生数据</p>';
                }
            } catch (error) {
                // 隐藏加载状态
                loadingDiv.style.display = 'none';
                
                // 显示错误信息
                resultDiv.innerHTML = `<div class="error">获取数据失败: ${error.message}</div>`;
                console.error('获取学生数据时出错:', error);
            }
        }
        
        function displayStudents(students) {
            const resultDiv = document.getElementById('result');
            
            let html = '<table>';
            html += '<tr><th>ID</th><th>姓名</th><th>年龄</th><th>专业</th></tr>';
            
            students.forEach(student => {
                html += `<tr>
                    <td>${student.id}</td>
                    <td>${student.name}</td>
                    <td>${student.age}</td>
                    <td>${student.major}</td>
                </tr>`;
            });
            
            html += '</table>';
            resultDiv.innerHTML = html;
        }
    </script>
</body>
</html>